package com.viaje.action.ranking;

import org.apache.struts2.convention.annotation.*;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.*;

import javax.persistence.*;

import com.viaje.common.util.Utilitario;
import com.viaje.model.Ranking;

public class CrearRankingAction {
private static final long serialVersionUID = 1L;
	
	private Ranking ranking;
	private String message;
	@Action(value="/registrar-ranking",
			results={
					@Result(name="exito",type="tiles",location="message.tiles"),
					@Result(name="error",type="tiles",location="registrar-ranking.tiles"),
					@Result(name="input",type="tiles",location="registrar-ranking.tiles")},
					interceptorRefs={
					 @InterceptorRef("fileUpload"), 
					 @InterceptorRef("basicStack"),
					 @InterceptorRef("jsonValidationWorkflowStack")}
			)
	@Validations(
		       requiredStrings =
		        	{  @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "ranking.nombre", key = "registrar.ranking.nombre.required"),
		    		   @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "ranking.puesto", key = "registrar.ranking.puesto.required"),  		   
		        	})
	public String execute(){
		String result="exito";
		message = "Se registro la lista de rankings";	
			EntityManager em= Utilitario.getInstance().getEntityManager();
			EntityTransaction tx=em.getTransaction();	
			try{
				tx.begin();
				em.persist(ranking);
				message="Registrado satisfactoriamente";
				result="exito";
				tx.commit();
				ranking.setNombre("");
				ranking.setPuesto(0);
				
			}catch (Exception e) {
				e.printStackTrace();
				tx.rollback();
				message="Intente otra ves";
				result="error";
			}//TRY
		//}//IF EMPTY
		return result;	
	}//execute

}
